package com.ssi.mrms.action.user;

import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;

import com.ssi.mrms.action.BaseAction;
import com.ssi.mrms.model.User;
import com.ssi.mrms.service.ComboboxService;
import com.ssi.mrms.service.UserService;
import com.ssi.mrms.view.SelectBoxView;


@Namespaces(value = { @Namespace("/user") })
public class USR001AAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(USR001AAction.class);
	
	private ComboboxService comboboxService  = lookupBean(ComboboxService.class, "ComboboxService");
	private UserService userService = lookupBean(UserService.class, "UserService");
	private List<SelectBoxView> comboRole = null;
	private User user = null;
	
	@Action(value = "/USR001A", results = { @Result(name = "success", location = "USR001A.tiles", type = "tiles") })
	public String execute() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
		
		setComboRole(comboboxService.getActiveRole());
		setUser(userService.findByUsername(user==null? null: user.getUserName()==null? null:user.getUserName()));
		
		logger.info("USR001A execute lists >>>> user: "+ToStringBuilder.reflectionToString(user));

		return SUCCESS;
	}
	
	@Action(value = "/saveOrUpdate", results = { @Result(name = "success", location="USR001", type = "redirect" , params = {"msg.msg", "S001"}) })
	public String save() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
		logger.info("USR001A saveOrUpdate >>>> user: "+ToStringBuilder.reflectionToString(user));
		
		if(null ==user.getUserId()){
			userService.save(user);
		}else{
			userService.update(user);
		}
		
		return SUCCESS;
	}
	
	@Action(value = "USR001Adelete", results = { @Result(name = "success", location="USR001", type = "redirect" ,params = {"msg.msg", "D001"}) })
	public String delete() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
		logger.info("USR001A delete >>>> user: "+ToStringBuilder.reflectionToString(user));
		
		if(null !=user.getUserName()){
			userService.delete(user);
		}else{
		 throw new RuntimeException(" Not Found Username !");
		}
		
		return SUCCESS;
	}

	public List<SelectBoxView> getComboRole() {
		return comboRole;
	}

	public void setComboRole(List<SelectBoxView> comboRole) {
		this.comboRole = comboRole;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


}
